﻿using System;
using System.Xml.Serialization;
using TallyJ1Announcer.VoterRelated;
using TallyJ1Announcer.Properties;

namespace TallyJ1Announcer.XmlSource
{
	[XmlRoot("Person")]
	public class XmlPerson
	{
		#region XmlAgeGroup enum

		public enum XmlAgeGroup
		{
			Adult,
			Youth,
			Child,
			PreYouth
		}

		#endregion

		#region XmlVoted enum

		public enum XmlVoted
		{
			No,
			VotedInPerson,
			DroppedOff,
			Mailed
		}

		#endregion

		#region XmlVoterEligibility enum

		public enum XmlVoterEligibility
		{
			Eligible,
			Unknown,
			Ineligible
		}

		#endregion

		[XmlAttribute(AttributeName = "Voted")]
		public XmlVoted Voted { get; set; }

		[XmlAttribute(AttributeName = "EnvNum")]
		public string EnvNum { get; set; }

		[XmlAttribute(AttributeName = "BahaiId")]
		public string BahaiId { get; set; }

		[XmlAttribute(AttributeName = "FName")]
		public string FirstName { get; set; }

		[XmlAttribute(AttributeName = "LName")]
		public string LastName { get; set; }

		[XmlAttribute(AttributeName = "AKAName")]
		public string AKAName { get; set; }

		[XmlAttribute(AttributeName = "AgeGroup")]
		public XmlAgeGroup AgeGroup { get; set; }

		public XmlPerson()
		{
			// defaults
			IneligibleToReceiveVotes = false;
			AgeGroup = XmlAgeGroup.Adult;
		}

		[XmlAttribute(AttributeName = "IneligibleToReceiveVotes")]
		public bool IneligibleToReceiveVotes { get; set; }

		///<summary>Map to core Person</summary>
		///<returns></returns>
		public Voter AsName()
		{
			return new Voter(FirstName + " " + LastName,
			                Voted.Formatted(EnvNum),
			                string.Format("{0}_{1}_{2}_{3}", LastName, FirstName, AKAName, BahaiId),
			                false);
		}
	}
	public static class LocalExtension
	{
		public static string Formatted(this XmlPerson.XmlVoted input, string envNum)
		{
			var suffix = envNum.HasContent() ? "  #" + envNum : string.Empty;

			switch (input)
			{
				case XmlPerson.XmlVoted.No:
					return "-";

				case XmlPerson.XmlVoted.VotedInPerson:
					return Resources.VotingMethod_VotedInPerson;
					
				case XmlPerson.XmlVoted.DroppedOff:
					return Resources.VotingMethod_DroppedOff + suffix;
					
				case XmlPerson.XmlVoted.Mailed:
					return Resources.VotingMethod_Mailed + suffix;
					
				default:
					throw new ArgumentOutOfRangeException("input");
			}
		}

	}
}